package com.zheng.hr.rpc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.hr.dao.mapper.HrDictMapper;
import com.zheng.hr.dao.model.HrDict;
import com.zheng.hr.rpc.api.HrDictService;
import com.zheng.oa.common.exception.OaException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-01-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class HrDictServiceImpl extends BaseServiceImpl<HrDictMapper, HrDict> implements HrDictService {
    @Override
    public HrDict getDictByValue(String value, String key) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("value='" + value + "'").and("key_name='" + key + "'");
        return selectOne(dictEntityWrapper);
    }

    @Override
    public HrDict getDictByName(String name, String key) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("name='" + name + "'").where("key_name='" + key + "'");
        return selectOne(dictEntityWrapper);
    }

    @Override
    public String getDictName(String value, String key, String defaultValue) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("value='" + value + "'").where("key_name='" + key + "'");
        HrDict dict = selectOne(dictEntityWrapper);
        if (null != dict && StringUtils.isNotEmpty(dict.getName())) {
            return dict.getName();
        }
        dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("value='" + defaultValue + "'").where("key_name='" + key + "'");
        dict = selectOne(dictEntityWrapper);
        if (null != dict) {
            return dict.getName();
        }
        return null;
    }

    @Override
    public List<String> getDictNames(String[] values, String key) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.and("key_name=" + key + "'");
        dictEntityWrapper.in("value", values);
        List<HrDict> dictList = selectList(dictEntityWrapper);
        List<String> nameList = new ArrayList<>();
        for (HrDict dict : dictList) {
            nameList.add(dict.getName());
        }
        return nameList;
    }

    @Override
    public String getDictValue(String name, String key, String defaultName) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("name='" + name + "'").where("key_name='" + key + "'");
        HrDict dict = selectOne(dictEntityWrapper);
        if (null != dict && StringUtils.isNotEmpty(dict.getValue())) {
            return dict.getValue();
        }
        dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("name='" + defaultName + "'").where("key_name='" + key + "'");
        dict = selectOne(dictEntityWrapper);
        if (null != dict && StringUtils.isNotEmpty(dict.getValue())) {
            return dict.getValue();
        }
        return null;
    }

    public String getValue(String name, String key, String defaultValue) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("name='" + name + "'").where("key_name='" + key + "'");
        HrDict dict = selectOne(dictEntityWrapper);
        if (null != dict && StringUtils.isNotEmpty(dict.getValue())) {
            return dict.getValue();
        } else {
            return defaultValue;
        }
    }

    @Override
    public List<HrDict> getDictList(String key) {
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("key_name='" + key + "'");
        return selectList(dictEntityWrapper);
    }

    @Override
    public int selectMaxSort() {
        return super.baseMapper.selectMaxSort();
    }

    @Override
    public boolean createDict(JSONArray dictArray) throws OaException {
        int sort = selectMaxSort() + 1;
        List<HrDict> dictList = dictArray.toJavaList(HrDict.class);
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("key_name='" + dictList.get(0).getKeyName() + "'");

        if (selectOne(dictEntityWrapper) != null) {
            throw new OaException("添加失败,已有相同key为" + dictList.get(0).getKeyName() + "的数据字典");
        }
        for (HrDict dict : dictList) {
            dict.setSort(sort);
            dictEntityWrapper = new EntityWrapper<>();
            dictEntityWrapper.where("key_name='" + dict.getKeyName() + "'").where("value='" + dict.getValue() + "'");
            if (selectOne(dictEntityWrapper) != null) {
                throw new OaException("添加失败,已添加相同value为" + dict.getValue() + "的字段");
            }
            if (!insert(dict)) {
                throw new OaException("添加失败");
            }
        }
        return true;
    }

    @Override
    public boolean updateDict(List<HrDict> newDictList, String oldKey) throws OaException {
        List<HrDict> dictList = getDictList(oldKey);
        Integer sort = dictList.get(0).getSort();
        String newKey = dictList.get(0).getKeyName();
        EntityWrapper<HrDict> dictEntityWrapper = new EntityWrapper<>();

        if (!newKey.equals(oldKey)) {
            dictEntityWrapper.where("key_name='" + newKey + "'");
            if (selectOne(dictEntityWrapper) != null) {
                throw new OaException("修改失败,已有key为" + newKey + "的数据字典");
            }
        }
        for (HrDict dict : dictList) {
            if (!deleteById((dict.getId()))) {
                throw new OaException("删除旧的数据字典失败！");
            }
        }

        for (HrDict dict : newDictList) {
            dict.setSort(sort);
            dictEntityWrapper = new EntityWrapper<>();
            dictEntityWrapper.where("key_name='" + dict.getKeyName() + "'").where("value='" + dict.getValue() + "'");
            if (selectOne(dictEntityWrapper) != null) {
                throw new OaException("添加失败,已添加相同value为" + dict.getValue() + "的字段");
            }
            if (!insert(dict)) {
                throw new OaException("修改失败");
            }
        }
        return true;
    }

    @Override
    public int selectDictCount(String search) {
        return super.baseMapper.selectDictCount(search);
    }

    @Override
    public boolean deleteBatchDict(String ids) throws OaException {
        String key;
        EntityWrapper<HrDict> dictEntityWrapper;
        String[] idArray = ids.split("-");
        for (String idStr : idArray) {
            int id = Integer.parseInt(idStr);
            key = selectById(id).getKeyName();
            dictEntityWrapper = new EntityWrapper<>();
            dictEntityWrapper.where("key_name='" + key + "'");
            if (!delete(dictEntityWrapper)) {
                throw new OaException("删除失败");
            }
        }
        return true;
    }
}
